const CryptoJS = require('crypto-js')

// 加密
function aesEncry(sessionId, msg) {
    sessionId = sessionId.split('').reduce(function (accumulator, currentValue, currentIndex) {
        return currentIndex % 2 == 0 ? accumulator + '' : accumulator + currentValue;
    }, '');
    sessionId = CryptoJS.enc.Utf8.parse(sessionId);

    msg = "string" == typeof msg ? msg : JSON.stringify(msg);
    var encrypted = CryptoJS.AES.encrypt(msg, sessionId, {
        iv: sessionId,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    })
    var Dinfo = encrypted.toString();
    return encodeURIComponent(Dinfo);
}

// 解密
function aesDecry(sessionId, ciphertext) {
    sessionId = sessionId.split('').reduce(function (accumulator, currentValue, currentIndex) {
        return currentIndex % 2 == 0 ? accumulator + '' : accumulator + currentValue;
    }, '');
    sessionId = CryptoJS.enc.Utf8.parse(sessionId);

    var decryptedStr = CryptoJS.AES.decrypt(ciphertext, sessionId, {
        iv: sessionId,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    }).toString(CryptoJS.enc.Utf8)
    return JSON.parse(decryptedStr);
}



// {
//   level: 310,
//   isimgUrls: 1,
//   bgImgUrl: 'https://wos3.58cdn.com.cn/HimeDcBazHkN/captchaimg/c0c4d67d-4a1e-4be4-bee0-f15d12758fcb.png',
//   width: '280',
//   tip: '请点击并将滑块拖动到指定位置',
//   puzzleImgUrl: 'https://wos3.58cdn.com.cn/HimeDcBazHkN/captchaimg/a1ef765a-b1cb-45bc-a0b2-3bdb1de023d4.png',
//   type: '1'
// }

// var sessionId = '655354d615774cfcbd465dda09988f93';
// var msg = {
//     "sdkv": "3.0.1",
//     "busurl": "https://www.anjuke.com/captcha-verify/?callback=shield&from=antispam",
//     "useragent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
//     "clienttype": "1"
// }
// // console.log(aesEncry(sessionId, msg))
// sessionId = '930e70914d564c0888783d2595e267e2';
// var ciphertext = 'K4VRrEAEKo/WO8qlPFl98W/7E818wEHC1esyugL8X707vIUdCCgv197xHZqWlNfirVaKjjuxLXkPJzPa+ToBgU93PND7FgFP9SWgFWxxAhCruNfRwol0stNX9kv1KElie9Sdo3yPkjvKwNfg40yTe6rVp/ritokZVCeG1RZKf2GEWiCmML5vaUEUmtfAYW4JC2y8arObRyULVYdQSA20XH5dbxigPxCJBIJjGbOM2AtbkjKi+SjB8idn6it+fNsJzsXsC6rlnxe5Qkc1SP8Kv1+i823myvMBPdswUVAiXGXJD9qQnEU7YTLc2c1dmij9SHMspd7HYDlaXU7vhLE18tNoplzlF3W2eu2kkKTr6KVVgXuUwp2ISZSzqSDwKyyyEIuHwbBGYZLTgvs58tpnkDmunWz2NJQPK8QzyuyaWxw=';
//
// // console.log(aesDecry(sessionId, ciphertext))
// var track = {
//     "x": 88,
//     "track": "22,22,2|23,22,137|24,22,144|26,22,151|26,22,160|29,22,166|32,22,176|33,22,183|36,22,192|38,22,200|40,22,207|42,22,216|44,22,222|46,22,231|48,22,238|50,22,248|51,22,263|52,22,272|53,22,282|54,22,287|54,22,296|56,22,303|58,22,312|59,22,318|62,22,328|65,22,334|66,21,344|69,21,352|71,21,360|73,21,367|77,21,376|82,21,382|88,21,392|92,21,399|96,21,408|102,21,415|106,21,424|109,21,432|110,21,440|110,21,630|",
//     "p": [
//         0,
//         0
//     ]
// }
// sessionId = "1fd98ce0024d4f748b3244f1d5c803d8"
// console.log(aesEncry(sessionId, track))
